import Vue from 'vue'
import VueRouter from 'vue-router'
import notFound from "@/views/404.vue"
import Main from "@/views/main.vue"
import { getStorageUserInfo } from '@/utils'
Vue.use(VueRouter)
import "@/assets/common/commonStyle.scss"

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}
const publicRoutes = ['/login']
const routes = [
    {
        path: '/',
        name: 'home',
        component: Main,
        redirect: '/home',
        children: [
            {
                path: '/home',
                name: '主页',
                component: () => import('../views/home.vue'),
                meta: { permissions: [] },
            },
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/login.vue'),
    },
    {
        path: '/404',
        name: 'Page404',
        component: notFound
    },
    {
        path: '*',
        redirect: '/404'
    },
]

export const operatorRoutes = [
    {
        path: '/workshop',
        name: '工作台',
        component: Main,
        meta: {},
        icon: 'appstore',
        children: [
            {
                path: '/medHerbInfo',
                name: '药材产地信息',
                component: () => import('../views/appViews/medHerbInfo/index.vue'),
                meta: { permissions: ['medicinalPlaceOfOrigin'] },
            },
            {
                path: '/rowMaterialIncoming',
                name: '原料入库',
                component: () => import('../views/appViews/rowMaterialIncoming/index.vue'),
                meta: { permissions: ['rawEnterStorage'] },
            },
            {
                path: '/productionMgt',
                name: '生产管理',
                component: () => import('../views/appViews/productionMgt/index.vue'),
                meta: { permissions: ['productionManagement'] }
            },
            {
                path: '/inspectionMgt',
                name: '检验检测',
                component: () => import('../views/appViews/inspectionMgt/index.vue'),
                meta: { permissions: ['testDetection'] }
            },
            {
                path: '/packagingMgt',
                name: '包装管理',
                component: () => import('../views/appViews/packagingMgt/index.vue'),
                meta: { permissions: ['packagingManagement'] }
            },
            {
                path: '/auditSubMgt',
                name: '审核分包',
                component: () => import('../views/appViews/auditSubMgt/index.vue'),
                meta: { permissions: ['checkSubpackage'] }
            },
            {
                path: '/printMgt',
                name: '打印管理',
                component: () => import('../views/appViews/printMgt/index.vue'),
                meta: { permissions: ['printManagement'] }
            },
            {
                path: '/outsourcingManagement',
                name: '外包管理',
                component: () => import('../views/appViews/outsourcingManagement.vue'),
                meta: { permissions: ['outsourcingManagement'] }
            },
            {
                path: '/inventoryManagement',
                name: '库存管理',
                component: () => import('../views/appViews/inventoryManagement.vue'),
                meta: { permissions: ['inventoryManagement'] },
            },
            {
                path: '/sellMgt',
                name: '销售管理',
                component: () => import('../views/appViews/sellMgt/index.vue'),
                meta: { permissions: ['sellManagement'] }
            },
            {
                path:'/tracedInquire',
                name:'追溯查询',
                component: () => import('../views/appViews/tracedInquire/index.vue'),
                meta: { permissions: ["retrospectInquire"] }
            },
            // {
            //     path: '/productInMgt',
            //     name: '成品入库',
            //     component: () => import('../views/appViews/productInMgt/index.vue'),
            //     meta: { permissions: ['finishedPEnterStorage'] }
            // },
            // {
            //     path: '/productOutMgt',
            //     name: '成品出库',
            //     component: () => import('../views/appViews/productOutMgt/index.vue'),
            //     meta: { permissions: ['finishedPOutStorage'] }
            // },
        ],
    },
    {
        path: '/setting',
        name: '系统设置',
        icon: 'setting',
        component: Main,
        meta: {},
        children: [
            {
                path: '/companyInfo',
                name: '企业信息',
                component: () => import('../views/settingView/companyInfo.vue'),
                meta: { permissions: ['enterpriseInformation'] },
            },
            {
                path: '/memberManagement',
                name: '员工管理',
                component: () => import('../views/settingView/memberManagement.vue'),
                meta: { permissions: ['staffManagement'] },
            },
            {
                path: '/permissionsManagement',
                name: '权限配置',
                component: () => import('../views/settingView/permissionsManagement.vue'),
                meta: { permissions: ['authorityManagement'] },
            },
            {
                path: '/supplierMgt',
                name: '供应商管理',
                component: () => import('../views/settingView/supplierMgt/index.vue'),
                meta: { permissions: ['supplierManagement'] },
            },
            {
                path: '/customerMgt',
                name: '客户管理',
                component: () => import('../views/settingView/customerMgt/index.vue'),
                meta: {
                    permissions: ['customerManagement']
                },
            },
            {
                path: '/materialMgt',
                name: '档案管理',
                component: () => import('../views/settingView/materialMgt/index.vue'),
                meta: { permissions: ['materialManagement'] },
            },
            {
                path: '/checkBasisMgt',
                name: '执行标准',
                component: () => import('../views/settingView/checkBasisMgt/index.vue'),
                meta: { permissions: ['detectionPrinciple'] },
            },
            {
                path: '/testingMgt',
                name: '检测机构',
                component: () => import('../views/settingView/testingMgt/index.vue'),
                meta: { permissions: ['testingOrganization'] },
            },
            {
                path: '/workProcessMgt',
                name: '工艺工序',
                component: () => import('../views/settingView/workProcessMgt/index.vue'),
                meta: {
                    permissions: ['craftProcess']
                },
            },
            {
                path: '/unitMgt',
                name: '单位管理',
                component: () => import('../views/settingView/unitMgt/index.vue'),
                meta: {
                    permissions: ['unitManagement']
                },
            },
            {
                path: '/licenceMgt',
                name: '生产许可证',
                component: () => import('../views/settingView/licenceMgt/index.vue'),
                meta: {
                    permissions: ['productionCertificate']
                },
            },
            {
                path: '/warehouseMgt',
                name: '仓库管理',
                component: () => import('../views/settingView/warehouseMgt/index.vue'),
                meta: {
                    permissions: ['storeManagement']
                },
            },
            {
                path: '/productVolume',
                name: '产品大类装量管理',
                component: () => import('../views/settingView/productVolumeMgt/index.vue'),
                meta: {
                    permissions: ['productKindDiscretion']
                },
            },
        ],
    },
]


const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes: routes
})

router.beforeEach((to, from, next) => {
    let userInfo = getStorageUserInfo()
    console.info(to)
    if (userInfo && userInfo.id) {
        //已登录
        next()
    } else if (publicRoutes.indexOf(to.path) !== -1) {
        //公开路由
        next()
    } else {
        //
        next('/login')
    }

});

router.afterEach(route => {
    //
});
export const setRouter = (callback) => {
    const list = generateRoutes()
    for (let r of list) {
        router.addRoute(r)
    }
    callback()
}
const hasPermission = (permissions, route) => {
    if (route.meta && route.meta.permissions) {
        return permissions.some(permission => route.meta.permissions.includes(permission))
    } else {
        return true
    }
}
const filterAsyncRoutesWithPermissions = (routes, permissions) => {
    const res = []
    routes.forEach(route => {
        const r = { ...route }
        if (hasPermission(permissions, r)) {
            if (r.children) r.children = filterAsyncRoutesWithPermissions(r.children, permissions)
            res.push(r)
        }
    })
    return res
}
export const generateRoutes = () => {
    const userInfo = getStorageUserInfo()

    if (userInfo && userInfo.id) {
        const permissions = userInfo.resourceChildren.map(item => item.name)
        return filterAsyncRoutesWithPermissions(operatorRoutes, permissions)
    }
    return []
}
export default router
